home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / ptv1n2.arc / SAMPLE.C < prev    next >
C/C++ Source or Header  |  1990-06-14  |  8KB  |  246 lines

  1. /*
  2.    SAMPLE.C - Written by Jake Richter, Panacea Inc.
  3.  
  4.    (Minor portions of this code appear in "Programming the 8514/A"
  5.     from M&T Publishing)
  6.  
  7.  
  8.    This code demonstrates the use of the IBM Adapter Interface.
  9.  
  10.  
  11.    This program can be run by compiling it with the
  12.    following line using MSC v5.1:
  13.  
  14.          CL -c SAMPLE.C
  15.          LINK SAMPLE.C CALLAFI.OBJ
  16.  
  17.  
  18. */
  19. /*---------------------------------------------------------------------------
  20.  
  21.    Include file section
  22.  
  23.    Include everything but the kitchen sink.
  24.  
  25. ---------------------------------------------------------------------------*/
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #include <errno.h>
  30. #include <fcntl.h>
  31. #include <dos.h>
  32. #include <conio.h>
  33. #include <io.h>
  34. #include <malloc.h>
  35. #include <sys\types.h>
  36. #include <sys\stat.h>
  37. #include "ibmafi.h"     /* Used for AI info. Includes AFIDATA.H   */
  38.  
  39. /*---------------------------------------------------------------------------
  40.  
  41.    Global and Static definitions
  42.  
  43. ---------------------------------------------------------------------------*/
  44. static   HSCOL_DATA  hscol = {4, 0};
  45.  
  46.  
  47. /*---------------------------------------------------------------------------
  48.  
  49.    Code
  50.  
  51. ---------------------------------------------------------------------------*/
  52. /*---------------------------------------------------------------------------
  53.  
  54.    FillRect(x, y, width, height, color)
  55.  
  56.    Fill a "width"x"height" rectangle with "color" at location "x,y".
  57.    Use current drawing environment.
  58.  
  59. ---------------------------------------------------------------------------*/
  60. void  FillRect(x, y, width, height, color)
  61. int   x, y, width, height, color;
  62. {
  63.                               /* Define the data structure      */
  64. static   HRECT_DATA     rect = {8, 0, 0, 0, 0};
  65.  
  66.    hscol.index = color;       /* Set color passed value         */
  67.    HSCOL(&hscol);
  68.  
  69.    rect.coord.x_coord = x;
  70.    rect.coord.y_coord = y;
  71.    rect.width = width;
  72.    rect.height = height;      /* Set rectangle parameter block  */
  73.    HRECT(&rect);              /* Fill the rectangle             */
  74.  
  75.    return;
  76. }
  77.  
  78. /*---------------------------------------------------------------------------
  79.  
  80.    DrawLine(x1, y1, x2, y2, color)
  81.  
  82.    Draws a line from "x1,y1" to "x2,y2" in color.
  83.  
  84. ---------------------------------------------------------------------------*/
  85. void  DrawLine(x1, y1, x2, y2, color)
  86. int   x1, y1, x2, y2, color;
  87. {
  88.                               /* Define the data structure     */
  89. static   HLINE_DATA(2)  line = {8, 0, 0, 0, 0};
  90.  
  91.    hscol.index = color;          /* Set color passed value     */
  92.    HSCOL(&hscol);
  93.  
  94.    line.coords[0].x_coord = x1;  /* Set the first coordinate   */
  95.    line.coords[0].y_coord = y1;
  96.    line.coords[1].x_coord = x2;
  97.    line.coords[1].y_coord = y2;  /* Set the second coordinate  */
  98.  
  99.    HLINE(&line);                 /* Draw the line              */
  100.  
  101.    return;
  102. }
  103.  
  104. /*---------------------------------------------------------------------------
  105.  
  106.    main(argc, argv)
  107.  
  108.    Where all the action happens. No passed parameters are used.
  109.  
  110. ---------------------------------------------------------------------------*/
  111. main(argc, argv)
  112. int   argc;
  113. char  **argv;
  114. {
  115. /*
  116.    Allocate the data areas for the HQDPS, HOPEN, and HINIT
  117.    commands. The non-zero values for initialization are
  118.    the parameter block sizes. The structure definitions for
  119.    HQDPS_DATA, HOPEN_DATA, HCLOSE_DATA, and HINIT_DATA are
  120.    located in AFIDATA.H.
  121. */
  122. static HQDPS_DATA       stateInfo = { 6, 0, 0, 0 };
  123. static HOPEN_DATA       openData = { 3, 0, 0 };
  124. static HINIT_DATA       stateData = { 2, 0 };
  125. static HCLOSE_DATA      closeData = { 2, 1 };
  126.  
  127.  
  128.    char     *stateBuffer; /* Pointer to task buffer */
  129.    char far *statAddr;    /* Pointer to task buffer */
  130.    int      i, j, x, y, count;
  131.    int      deltaX, deltaY;
  132.  
  133.  
  134.    if(!getafi())              /* Is AI loaded? */
  135.       {
  136.       printf("Error! No Adapter Interface Loaded.\n");
  137.       exit(1);
  138.       }
  139.  
  140.    /*
  141.       Get the size of the state buffer. The C call "HQDPS" is
  142.       actually a macro definition in IBMAFI.H. This applies
  143.       to all the other AI calls as well.
  144.    */
  145.    HQDPS(&stateInfo);
  146.  
  147.    /*
  148.       Use the state buffer size to dynamically allocate
  149.       memory for the state buffer. The "+ 15" is necessary
  150.       because HINIT requires the segment of the state buffer,
  151.       and the buffer is assumed to start at offset 0 of that
  152.       segment. By adding 15 to the size, we make sure that we
  153.       have allocated enough space for the buffer even in the
  154.       worst case situation in which the segment alignment is
  155.       furthest off. Remember that segment alignment occurs at
  156.       16 byte intervals where the linear address' least
  157.       significant nibble is 0.
  158.    */
  159.    stateBuffer = (char *) malloc(stateInfo.size + 15);
  160.    statAddr = (char far *) stateBuffer;
  161.  
  162.    /*
  163.       Get the segment of the state buffer. Make sure to
  164.       account for an allocation that is not segment boundary
  165.       aligned.
  166.    */
  167.    stateData.segment = FP_SEG(statAddr) + ((FP_OFF(statAddr) + 15) >> 4);
  168.  
  169.    /*
  170.       Initialize the parameters for the HOPEN command and
  171.       make the call. The HOPEN command opens the AI for use.
  172.       After the call has returned, check the return flags for
  173.       an error condition.
  174.    */
  175.    openData.oflags = 0x00;
  176.    openData.mode = 0;    /* Set the 1024x768 mode */
  177.    HOPEN(&openData);
  178.    if (openData.iflags)
  179.       {
  180.       printf("An error occurred while trying to open the AI!\n");
  181.       exit(2);
  182.       }
  183.  
  184.    /*
  185.       No error occurred, so get the state information so we
  186.       can start drawing.
  187.    */
  188.    HINIT(&stateData);
  189.  
  190.    /*
  191.       Now it's time to draw some things. First, let's "bounce"
  192.       filled rectangles around the display.
  193.    */
  194.    x = y = 0;        /* Start position */
  195.    deltaX = 11;
  196.    deltaY = 17;      /* Indicates rectangle velocity */
  197.    while (!kbhit())  /* Exit when a key is pressed */
  198.       {
  199.       count = 5000;  /* 5000 rectangles */
  200.       while (count--)
  201.          {           /* Draw a 40x20 rectangle at x,y in diff. colors */
  202.          FillRect(x, y, 40, 20, (count & 0x0F));
  203.          x += deltaX;
  204.          if (x < 0 || x > 983)
  205.             {        /* If either edge goes too far, change direction */
  206.             deltaX *= -1;
  207.             x += deltaX;
  208.             }
  209.          y += deltaY;
  210.          if (y < 0 || y > 747)
  211.             {        /* If either edge goes too far, change direction */
  212.             deltaY *= -1;
  213.             y += deltaY;
  214.             }
  215.          }
  216.    /*
  217.       Now let's draw some line sweeps to create moire patterns, but
  218.       use the last rectangle point as our "center".
  219.    */
  220.       for (i = 0; i < 1024; i += 2)
  221.          {
  222.          DrawLine(x, y, i, 0, i & 0x0F);
  223.          DrawLine(x, y, 1023 - i, 767, i & 0x0F);
  224.          }
  225.       for (i = 0; i < 768; i += 2)
  226.          {
  227.          DrawLine(x, y, 0, i, i & 0x0F);
  228.          DrawLine(x, y, 1023, 767 - i, i & 0x0F);
  229.          }
  230.       }              /* Do the whole thing over again */
  231.  
  232.    if (!getch())     /* If function key, get second code */
  233.       getch();
  234.  
  235.    /*
  236.       Now that we are done drawing, it's time to clean up
  237.       after ourselves. This includes closing the AI and
  238.       deallocating the state buffer.
  239.    */
  240.    HCLOSE(&closeData);
  241.    free(stateBuffer);
  242.  
  243.    return(0);        /* No errors to report */
  244. }
  245.  
  246.